﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;

using WpfApp4.Library;
using WpfApp4.Models;
using WpfApp4.Models.Config;
using WpfApp4.Utilities;
using WpfApp4.Views;

using XEvent;
using XEvent.Interface;

namespace WpfApp4
{
    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App
    {
        private Thread _coroutineThread;
        private readonly object _lock = new object();
        private bool _flag = false;

        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            ApplicationConfigSetting.DefaultUrl = ConfigurationManager.AppSettings[AppConfigKeys.DefaultUrl];
            ApplicationConfigSetting.DefaultWidth = Convert.ToInt32(ConfigurationManager.AppSettings[AppConfigKeys.DefaultWidth]);
            ApplicationConfigSetting.DefaultHeight = Convert.ToInt32(ConfigurationManager.AppSettings[AppConfigKeys.DefaultHeight]);
            ApplicationConfigSetting.CurrentUrl = ApplicationConfigSetting.DefaultUrl;

            MenuInfo info = ConfigManager.LoadConfig();
            MenuInfo.Instance.MenuItems = info.MenuItems ?? new MenuConfig[] { };
            MenuInfo.Instance.Settings = info.Settings ?? new SettingConfig();


            //_flag = true;

            //// 用多线程启动协程
            //_coroutineThread = new Thread(() =>
            //{
            //    while (GetFlag())
            //    {
            //        Time.Tick();
            //        CoroutineManager.Instance.Run();
            //    }
            //});
            //_coroutineThread.Start();
        }

        public override void RegisterTypes(IXContainer container)
        {
            container.Register<SettingWindow>("settingWindow");
            container.Register<TestWindow>("test");
            container.Register<NewSettingWindow>("setting");
            container.Register<BrowseWindow>("browse");
            container.Register<InputWindow>("inputBox");
        }

        protected override void OnExit(ExitEventArgs e)
        {
            base.OnExit(e);
            //lock (_lock)
            //{
            //    _flag = false;
            //}

            //if(_coroutineThread != null)
            //{
            //    _coroutineThread = null;
            //}
            //ConfigManager.SaveConfig();
        }

        //private bool GetFlag()
        //{
        //    //bool flag;
        //    //lock (_lock)
        //    //{
        //    //    flag = _flag;
        //    //}
        //    //return flag;
        //}
    }
}
